package com.jemaa.news.app;

import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class PageNews {
	
	//Data.
	private List<News> newsList;
	private int totalRows;
	
	//News category
	private int categ;
	

	// Paging.
    private int firstRow;
    private int rowsPerPage;
    private int totalPages;
    private int pageRange;
    private Integer[] pages;
    private int currentPage;


	public PageNews() {
        // Set default values somehow (properties files?).
        rowsPerPage = 5; // Default rows per page (max amount of rows to be displayed at once).
        pageRange = 10; // Default page range (max amount of page links to be displayed at once).
        categ = 0; //Default category of news (all mean that all news category will be displayed)
    }
    
    
 // Loaders ------------------------------------------------------------------------------------
    public void reset(){
    	rowsPerPage = 5;
    	pageRange = 10;
    }
    
    public void updateFields() {
		
		// Set currentPage, totalPages and pages.
        currentPage = (totalRows / rowsPerPage) - ((totalRows - firstRow) / rowsPerPage) + 1;
        totalPages = (totalRows / rowsPerPage) + ((totalRows % rowsPerPage != 0) ? 1 : 0);
        int pagesLength = Math.min(pageRange, totalPages);
        pages = new Integer[pagesLength];

        // firstPage must be greater than 0 and lesser than totalPages-pageLength.
        int firstPage = Math.min(Math.max(0, currentPage - (pageRange / 2)), totalPages - pagesLength);

        // Create pages (page numbers for page links).
        for (int i = 0; i < pagesLength; i++) {
            pages[i] = ++firstPage;
        }
	}
    
    
 // Getters ------------------------------------------------------------------------------------

    public List<News> getNewsList() {
        return newsList;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public int getFirstRow() {
        return firstRow;
    }

    public int getRowsPerPage() {
        return rowsPerPage;
    }

    public Integer[] getPages() {
        return pages;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPages() {
        return totalPages;
    }
    
    public int getCateg() {
		return categ;
	}

    // Setters ------------------------------------------------------------------------------------

    public void setRowsPerPage(int rowsPerPage) {
        this.rowsPerPage = rowsPerPage;
    }

	public void setFirstRow(int firstRow) {
		this.firstRow = firstRow;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public void setCateg(int categ) {
		if(categ>=0 && categ <=5){
			this.categ = categ;
		}
	}
	
}
